//========================================================================================
//  
//  $File: $
//  
//  Owner: Toan Nguyen
//  
//  $Author: $
//  
//  $DateTime: $
//  
//  $Revision: $
//  
//  $Change: $
//  
//  Copyright 1997-2008 Adobe Systems Incorporated. All rights reserved.
//  
//  NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance 
//  with the terms of the Adobe license agreement accompanying it.  If you have received
//  this file from a source other than Adobe, then your use, modification, or 
//  distribution of it requires the prior written permission of Adobe.
//  
//========================================================================================

#include "VCPlugInHeaders.h"

// Interface includes:
#include "IActiveContext.h"
#include "ITextEditSuite.h" 
#include "ISelectionManager.h"
#include "IPanelControlData.h" 
#include "IDropDownListController.h"

// General includes:
#include "CDialogController.h"
#include "StringUtils.h"
// Project includes:
#include <string>
#include "AMPID.h"
#include "HTTPRequestHelper.h"
#include "AMGlobal.h"
#include "IWidgetParent.h"
#include "ITextControlData.h"
#include "AMPCAlert.h"
/** DlgCMSLoginDialogController
	Methods allow for the initialization, validation, and application of dialog widget
	values.
	Implements IDialogController based on the partial implementation CDialogController.

	
	@ingroup writefishprice
*/
class DlgCMSLoginDialogController : public CDialogController
{
	public:
		/** Constructor.
			@param boss interface ptr from boss object on which this interface is aggregated.
		*/
		DlgCMSLoginDialogController(IPMUnknown* boss) : CDialogController(boss) {}

		/** Destructor.
		*/
		virtual ~DlgCMSLoginDialogController() {}

		/** Initialize each widget in the dialog with its default value.
			Called when the dialog is opened.
		*/
	    virtual void InitializeDialogFields(IActiveContext* dlgContext);

		/** Validate the values in the widgets.
			By default, the widget with ID kOKButtonWidgetID causes
			ValidateFields to be called. When all widgets are valid,
			ApplyFields will be called.
			@return kDefaultWidgetId if all widget values are valid, WidgetID of the widget to select otherwise.

		*/
	       virtual WidgetID ValidateDialogFields(IActiveContext* myContext);


		/** Retrieve the values from the widgets and act on them.
			@param widgetId identifies the widget on which to act.
		*/
		virtual void ApplyDialogFields(IActiveContext* myContext, const WidgetID& widgetId);
};

CREATE_PMINTERFACE(DlgCMSLoginDialogController, kDlgCMSLoginDialogControllerImpl)

/********************************************************************************************************************
 *****Function: InitializeDialogFields
 *****Creator:  Toan Nguyen
 *****IN: IActiveContext* dlgContext
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
void DlgCMSLoginDialogController::InitializeDialogFields(IActiveContext* dlgContext)
{
	CDialogController::InitializeDialogFields(dlgContext);

	if (g_Domain.length() == 0) {
		g_Domain = "http://bandt.b092.staging.audiencemedia.com";
	}
	PMString domainStr(g_Domain.c_str());
	PMString usernameStr(g_Username.c_str());
	PMString passwordStr(g_Password.c_str());
	SetTextControlData(kClientIDListWidgetID, domainStr);	//<--20120510
	SetTextControlData(kBscUserNameWidgetID, usernameStr);
	SetTextControlData(kBscPasswordWidgetID, passwordStr);
/*
	do {
		InterfacePtr<IWidgetParent> iWidgetParent(this, UseDefaultIID());
		ASSERT(iWidgetParent);
		if(!iWidgetParent)
		{
			break;
		}
		InterfacePtr<IPanelControlData>	iPanelControlData( 
														  (IPanelControlData*)iWidgetParent->QueryParentFor(IID_IPANELCONTROLDATA) );
		ASSERT(iPanelControlData);
		if(!iPanelControlData)
		{
			break;
		}
		IControlView* clusterView = iPanelControlData->FindWidget(kAMPClusterProxyWidgetID);
		ASSERT(clusterView);
		if(!clusterView)
		{
			break;
		}
		InterfacePtr<IPanelControlData> clusterPanel(clusterView, UseDefaultIID());
		if (!clusterPanel)
		{
			break;
		}
		IControlView* proxyPortView = clusterPanel->FindWidget(kAMPProxySettingWidgetID);
		if (proxyPortView == nil)
		{
			break;
		}
		if(g_ProxyEnable == FALSE)
		{
			IControlView* childView = clusterPanel->FindWidget(kAMPRadioAutoWidgetID);
			if (childView == nil)
			{
				break;
			}
			InterfacePtr<ITriStateControlData> triData(childView, UseDefaultIID());
			if (triData)
			{
				triData->SetState(ITriStateControlData::kSelected, nil, nil);
			}
			proxyPortView->Disable();
		}else
		{
			IControlView* childView = clusterPanel->FindWidget(kAMPRadioManualWidgetID);
			if (childView == nil)
			{
				break;
			}
			InterfacePtr<ITriStateControlData> triData(childView, UseDefaultIID());
			if (triData)
			{
				triData->SetState(ITriStateControlData::kSelected, nil, nil);
			}
			proxyPortView->Enable(TRUE, TRUE);
			InterfacePtr<ITextControlData> textControlData(proxyPortView, UseDefaultIID());
			ASSERT(textControlData);
			if(!textControlData)
			{
				break;
			}
			textControlData->SetString(g_ProxyPort.c_str());
		}
	
	}while(kFalse);
*/	
/*
	// STEP 9.1  - BEGIN
	do {
		// Get current panel control data.
		InterfacePtr<IPanelControlData> pPanelData(QueryIfNilElseAddRef(nil));
		if (pPanelData == nil)
		{
			ASSERT_FAIL("DlgCMSLoginDialogController::InitializeFields: PanelControlData is nil!");
			break;
		}
		else
		{
			// Find dropdown list menu control view from panel data.
			IControlView* pDropDownListControlView = pPanelData->FindWidget(kWFPDropDownListWidgetID);
			if (pDropDownListControlView == nil)
			{
				// Is the widget on the dialog?
				ASSERT_FAIL("DlgCMSLoginDialogController::InitializeFields: DDListCtrlView is nil!");
				break;
			}
			
			// Get IDropDownListController interface pointer.
			InterfacePtr<IDropDownListController> pDropDownListController(pDropDownListControlView, UseDefaultIID());
			if (pDropDownListController == nil)
			{
				// Is the controller available?
				ASSERT_FAIL("DlgCMSLoginDialogController::InitializeFields: DDListController is nil!");
				break;
			}
			
			// Select the element at the given position in the list.
			pDropDownListController->Select(0);
			
			// Initialize TextEditBox.
			PMString InitialString("");
			SetTextControlData(kWFPTextEditBoxWidgetID, InitialString);
		}
	} while (kFalse);
	// STEP 9.1  - END
*/
}

/********************************************************************************************************************
 *****Function: ValidateDialogFields
 *****Creator:  Toan Nguyen
 *****IN: IActiveContext* myContext
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
WidgetID DlgCMSLoginDialogController::ValidateDialogFields(IActiveContext* myContext)
{
	WidgetID result = CDialogController::ValidateDialogFields(myContext);
	// Put code to validate widget values here.
	return result;
}


/********************************************************************************************************************
 *****Function: ApplyDialogFields
 *****Creator:  Toan Nguyen
 *****IN: IActiveContext* myContext, const WidgetID& widgetId
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
void DlgCMSLoginDialogController::ApplyDialogFields(IActiveContext* myContext, const WidgetID& widgetId)
{
	// DropDownList result string.
	////cout << "DlgCMSLoginDialogController::ApplyDialogFields" << endl;
	PMString resultString;
	
	PMString nUsernameString = this->GetTextControlData(kBscUserNameWidgetID);
	PMString nPasswordString = this->GetTextControlData(kBscPasswordWidgetID);
	PMString nDomainString = this->GetTextControlData(kClientIDListWidgetID);

	//UsernameLogin = nUsernameString.GetPlatformString();
	//PasswordLogin = nPasswordString.GetPlatformString();
	//DomainLogin = nDomainString.GetPlatformString();
	string _domain = nDomainString.GetPlatformString();
	string _username = nUsernameString.GetPlatformString();
	g_Password = nPasswordString.GetPlatformString();
	
	size_t pos = _domain.find("http://");
	if (pos != string::npos) {
		_domain = _domain.substr(pos+7);
	}
	pos = _domain.find("/");
	if(pos != string::npos) {
		if((_domain.length() - 1) == pos){
			_domain = _domain.substr(0, pos);
			if(g_Domain != _domain){
				g_Package_id = "";
			}
			g_Domain = _domain;
			g_Username = _username;
		}else {
			AMPCAlert::ModalAlert
			(
			 PMString("\n\nLogin Failed. Please enter correct your domain"),	// Alert string
			 PMString("Yes"),			// OK button
			 kNullString,//PMString("No"),					// No second button
			 kNullString,
			 kNoNeedToWaitForDialog,		// Set OK button to default
			 1				// No icon.
			 );
			g_Username = "";
			g_Password = "";
		}
	}else{
		g_Domain = _domain;
		g_Username = _username;
	}
	////cout << "length domain:" << g_Domain.length() << endl;
	////cout << "length pos:" << pos << endl;
	g_ProxyAuthen = g_ProxyUser + ":" + g_ProxyPass;

	////cout << "Rootname: " << UsernameLogin << " RootPass: " << PasswordLogin << " Domain: " << DomainLogin << endl;
	/*
	std::string userString(nUsernameString.GetPlatformString());
	const char* myUserPointer = userString.c_str();
	std::string passString(nPasswordString.GetPlatformString());
	const char* myPassPointer = passString.c_str();
	
	//wchar_t szServerUsername[100];
	//wchar_t szServerPassword[100];
	//mbstowcs(szServerUsername, myUserPointer, strlen(myUserPointer));
	//mbstowcs(szServerPassword, myPassPointer, strlen(myPassPointer));

	mbstowcs(UsernameLogin, myUserPointer, strlen(myUserPointer)+1);
	mbstowcs(PasswordLogin, myPassPointer, strlen(myPassPointer)+1);
	*/
	 
	//Get Selected text of DropDownList.
	//PMString ClientID = this->GetTextControlData(kClientIDListWidgetID);	//<--20120510
	//ClientID.Translate(); // Look up our string and replace.
	//resultString.Translate(); // Look up our string and replace.
/*
	// Get the editbox list widget string.
	PMString editBoxString = this->GetTextControlData(kWFPTextEditBoxWidgetID);
swprintf(szBuff, L"%p", m_hWnd);
	PMString moneySign(kWFPStaticTextKey);
	moneySign.Translate(); // Look up our string and replace.
	
	resultString.Append('\t'); // Append tab code.
	resultString.Append(moneySign);
	resultString.Append(editBoxString);
	resultString.Append('\r'); // Append return code.

    if (myContext == nil)
	{
		ASSERT(myContext);
		return;
	}
	// Insert resultString to TextFrame.
    InterfacePtr<ITextEditSuite> textEditSuite(myContext->GetContextSelection(), UseDefaultIID());

    if (textEditSuite && textEditSuite->CanEditText())
    {
        ErrorCode status = textEditSuite->InsertText(WideString(resultString));
        ASSERT_MSG(status == kSuccess, "DlgCMSLoginDialogController::ApplyDialogFields: can't insert text"); 
    }
*/
}

//  Code generated by DollyXs code generator
